<?php
require_once('SakeStorage.php');

class UT3SakeStorage extends SakeStorage {
	protected $url = 'http://ut3pc.sake.gamespy.com/SakeStorageServer/StorageServer.asmx';
	protected $tableid = 'PlayerStats_v2';
	protected $gameid = 1727;
	
	//const DEFAULT_FILTER = '(NUM_PlayerDM > 0) OR (NUM_Pure_PlayerDM > 0)';
	
	const GENERAL = 0;
	const WEAPONS = 1;
	const VEHICLES = 2;
	
	static function addPure($field) {
		switch ($field) {
			case 'Nick':
			case 'ownerid':
			case 'recordid':
			case 'row':
            # there could be more yet I don't know
				break;
			default:
				if (strpos($field, 'NUM_') !== false) {
					$phpsucks = explode('NUM_', $field); #why I can't just explode(...)[1] ?
					$field = 'NUM_Pure_'.$phpsucks[1];
				} else {
					$field = 'Pure_'.$field;
				}
				break;
		}
		return $field;
	}
	
	// they aren't mandatory, it's just useful to include them every time
	static $CommonFields = array('ownerid', 'Nick');
	
	protected static $PlayerDM = array(
		'PlayerDM_ELO',
		'NUM_PlayerDM',
		'PlayerDM_EVENT_KILLS',
		'PlayerDM_EVENT_DEATHS',
		'PlayerDM_EVENT_BULLSEYE',
		'PlayerDM_EVENT_DENIEDREDEEMER',
		'PlayerDM_EVENT_EAGLEEYE',
		'PlayerDM_EVENT_ENDSPREE',
		'PlayerDM_EVENT_FIRSTBLOOD',
		'PlayerDM_EVENT_HIJACKED',
		'PlayerDM_EVENT_RANOVERKILLS',
		'PlayerDM_EVENT_RANOVERDEATHS',
		'PlayerDM_EVENT_TOPGUN',
		'PlayerDM_MULTIKILL_DOUBLEKILL',
		'PlayerDM_MULTIKILL_MEGAKILL',
		'PlayerDM_MULTIKILL_MONSTERKILL',
		'PlayerDM_MULTIKILL_MULTIKILL',
		'PlayerDM_MULTIKILL_ULTRAKILL',
		'PlayerDM_PICKUPS_BERSERK',
		'PlayerDM_PICKUPS_INVISIBILITY',
		'PlayerDM_PICKUPS_INVULNERABILITY',
		'PlayerDM_PICKUPS_UDAMAGE',
		'PlayerDM_PICKUPS_JUMPBOOTS',
		'PlayerDM_PICKUPS_SHIELDBELT',
		'PlayerDM_PICKUPS_ARMOR',
		'PlayerDM_PICKUPS_HEALTH',
		'PlayerDM_POWERUPTIME_BERSERK',
		'PlayerDM_POWERUPTIME_INVISIBILITY',
		'PlayerDM_POWERUPTIME_INVULNERABILITY',
		'PlayerDM_POWERUPTIME_UDAMAGE',
		'PlayerDM_REWARD_JACKHAMMER',
		'PlayerDM_REWARD_GUNSLINGER',
		'PlayerDM_REWARD_BIOHAZARD',
		'PlayerDM_REWARD_COMBOKING',
		'PlayerDM_REWARD_SHAFTMASTER',
		'PlayerDM_REWARD_BLUESTREAK',
		'PlayerDM_REWARD_FLAKMASTER',
		'PlayerDM_REWARD_ROCKETSCIENTIST',
		'PlayerDM_REWARD_HEADHUNTER',
		'PlayerDM_REWARD_BIGGAMEHUNTER',
		'PlayerDM_REWARD_ROADRAMPAGE',
		'PlayerDM_SPREE_DOMINATING',
		'PlayerDM_SPREE_GODLIKE',
		'PlayerDM_SPREE_KILLINGSPREE',
		'PlayerDM_SPREE_RAMPAGE',
		'PlayerDM_SPREE_MASSACRE',
		'PlayerDM_SPREE_UNSTOPPABLE',
		'PlayerDM_EVENT_HATTRICK',
		'PlayerDM_EVENT_LASTSECONDSAVE',
		'PlayerDM_EVENT_KILLEDFLAGCARRIER',
		'PlayerDM_EVENT_RETURNEDFLAG',
		'PlayerDM_EVENT_SCOREDFLAG',
		'PlayerDM_EVENT_RETURNEDORB',
		'PlayerDM_NODE_DAMAGEDCORE',
		'PlayerDM_NODE_DESTROYEDCORE',
		'PlayerDM_NODE_DESTROYEDNODE',
		'PlayerDM_NODE_HEALEDNODE',
		'PlayerDM_NODE_NODEBUILT',
		'PlayerDM_NODE_NODEBUSTER'
	);
	
	protected static $WeaponsDM = null;
	protected static $VehiclesDM = null;
	protected static $VehicleWeaponsDM = null;
		
	public static $brief = array(
		self::GENERAL => array('General', array(
			array('Events', 'EVENT'),
			array('Multikills', 'MULTIKILL'),
			array('Killing Sprees', 'SPREE'),
			array('Pickups', 'PICKUPS'),
			array('Pickup Times', 'POWERUPTIME'),
			array('Awards', 'REWARD'),
			array('Warfare', 'NODE')
		)),
		self::WEAPONS => array('Weapons', array(
			array('Translocator', 'TRANSLOCATOR'),
			array('Impact Hammer', 'IMPACTHAMMER'),
			array('Enforcer', 'ENFORCER'),
			array('Bio Rifle', 'BIORIFLE'),
			array('Shock Rifle', 'SHOCKRIFLE'),
			array('Shock Combo', 'SHOCKCOMBO'),
			array('Link Gun', 'LINKGUN'),
			array('Stinger Minigun', 'STINGER'),
			array('Flak Cannon', 'FLAKCANNON'),
			array('Rocket Launcher', 'ROCKETLAUNCHER'),
			array('Sniper Rifle', 'SNIPERRIFLE'),
			array('Headshot', 'HEADSHOT'),
			array('AVRiL Longbow', 'AVRIL'),
			array('Redeemer', 'REDEEMER'),
			array('Instagib Rifle', 'INSTAGIB'),
			array('Spider Mine', 'SPIDERMINE'),
			array('Shaped Charge', 'SHAPEDCHARGE'),
			array('Environment', 'ENVIRONMENT')
		)),
		self::VEHICLES => array('Vehicles', array(
			array('Manta', 'MANTA_CONTENT', array('MANTAGUN')),
			array('Scorpion', 'SCORPION_CONTENT', array('SCORPIONGLOB', 'SCORPIONBLADE', 'SCORPIONSELFDESTRUCT')),
			array('Hellbender', 'HELLBENDER_CONTENT', array('TURRETSHOCK', 'HELLBENDERPRIMARY')),
			array('Stealthbender', 'STEALTHBENDER_CONTENT', array('STEALTHBENDERGUN')),
			array('Paladin', 'PALADIN', array('PALADINGUN', 'PALADINEXPLOSION')),
			array('Goliath', 'GOLIATH_CONTENT', array('GOLIATHTURRET', 'GOLIATHMACHINEGUN')),
			array('Leviathan', 'LEVIATHAN_CONTENT', array('LEVIATHANPRIMARY', 'LEVIATHANEXPLOSION', 'LEVIATHANTURRETBEAM', 'LEVIATHANTURRETROCKET', 'LEVIATHANTURRETSTINGER', 'LEVIATHANTURRETSHOCK')),
			array('Raptor', 'RAPTOR_CONTENT', array('RAPTORGUN', 'RAPTORROCKET')),
			array('Cicada', 'CICADA_CONTENT', array('CICADAROCKET', 'CICADATURRET')),
			array('SPMA/Eradicator', 'SPMA_CONTENT', array('SPMATURRET', 'SPMACANNON', 'SPMACAMERACRUSH')),
			array('Viper', 'VIPER_CONTENT', array('VIPERGUN', 'VIPERSELFDESTRUCT')),
			array('Scavenger', 'SCAVENGER_CONTENT', array('SCAVENGERGUN', 'SCAVENGERSTABBED')),
			array('Nemesis', 'NEMESIS', array('NEMESISTURRET')),
			array('Darkwalker', 'DARKWALKER_CONTENT', array('DARKWALKERTURRET', 'DARKWALKERPASSGUN')),
			array('Fury', 'FURY_CONTENT', array('FURYGUN')),
			array('Nightshade', 'NIGHTSHADE_CONTENT', array('NIGHTSHADEGUN')),
			array('Hoverboard', 'HOVERBOARD', array()),
			array('Defense Turret', 'TURRET', array('TURRETPRIMARY', 'TURRETROCKET', 'TURRETSTINGER'))
		))
	);
		static public function getPlayerFields($forced = false) {
			return self::$PlayerDM;
		}
		
		static public function getWeaponsFields($forced = false) {
			if ($forced or is_null(self::$WeaponsDM)) {
				$return = array();
				foreach (self::$brief[self::WEAPONS][1] as $val) {
					$id = $val[1];
					foreach (array('KILLS', 'DEATHS', 'SUICIDES') as $type) {
						if (!($id === 'HEADSHOT' and $type === 'SUICIDES')) {
							$return[] = "WeaponsDM_{$type}_{$id}";
						}
					}
				}
				self::$WeaponsDM = $return;
			} else {
				$return = self::$WeaponsDM;
			}
			return $return;
		}
		
		static public function getVehiclesFields($forced = false) {
			if ($forced or is_null(self::$VehiclesDM)) {
				$return = array();
				foreach (self::$brief[self::VEHICLES][1] as $val) {
					$id = $val[1];
					foreach (array('DRIVING', 'VEHICLEKILL') as $type) {
						$return[] = "VehiclesDM_{$type}_UTVEHICLE_{$id}";
					}
				}
				self::$VehiclesDM = $return;
			} else {
				$return = self::$VehiclesDM;
			}
			return $return;
		}
		
		static function getVehicleWeaponsFields($forced = false) {
			if ($forced or is_null(self::$VehicleWeaponsDM)) {
				$return = array();
				foreach (self::$brief[self::VEHICLES][1] as $val) {
					$ids = $val[2];
					foreach ($ids as $id) {
						foreach (array('KILLS', 'DEATHS', 'SUICIDES') as $type) {
							$return[] = "VehicleWeaponsDM_{$type}_{$id}";
						}
					}
				}
				self::$VehicleWeaponsDM = $return;
			} else {
				$return = self::$VehicleWeaponsDM;
			}
			return $return;
		}
}
